<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2017/10/18
 * Time: 23:56
 */

namespace org\util\wordtool;


class Font
{
    protected $dom=null;

    protected $defaultFont=StaticElement::DEFAULT_FONT;
    protected $rPr=null;


    public function __construct()
    {
        $this->dom=new \DOMDocument();
        $this->rPr=$this->dom->createElement('w:rPr');
    }

    /**
     * @param array $font 字体数组
     * @return \DOMElement $this->rPr
     * @throws \Exception
     *
     * 参数：
     * name_zh:汉字字体
     * name_en:英文字体
     * name:通用字体
     * size:字号
     * bold 粗体
     * italic 斜体
     * super 上标
     * sub 下标
     * underline 下划线
     * Color 字体颜色
    */
    public function createRPR($font)
    {
        //字体
        $arr_font['name']=$this->setFontName($font);

        //字号
        if(!isset($font['size'])) $font['size']=$this->defaultFont['size'];
        //验证字号格式
        if(is_numeric($font['size'])){
            $size=2*$font['size'];
        }else{
            if(isset(StaticElement::FONT_SIZE[$font['size']])){
                $size=StaticElement::FONT_SIZE[$font['size']];
            }else{
                throw new \Exception('字号参数错误：'.$font['size']);
            }
        }

        $arr_font['size']=$this->dom->createElement('w:sz');
        $arr_font['sizeCs']=$this->dom->createElement('w:szCs');
        $arr_font['size']->setAttribute('w:val',$size)  ;
        $arr_font['sizeCs']->setAttribute('w:val',$size)  ;

        //粗体
        if (isset($font['bold'])){
            $arr_font['bold']=$this->dom->createElement('w:b');
            $arr_font['boldCs']=$this->dom->createElement('w:bCs');
        }

        //斜体
        if (isset($font['italic'])){
            $arr_font['italic']=$this->dom->createElement('w:i');
            $arr_font['italicCs']=$this->dom->createElement('w:iCs');
        }

        //下划线
        if (isset($font['underLine'])){
            $arr_font['underline']=$this->dom->createElement('w:u');
            $arr_font['underline']->setAttribute('w:val','single');
        }

        //文字颜色
        if (isset($font['color'])){
            $arr_font['color']=$this->dom->createElement('w:color');
            $color=Style::checkColor($font['color']);
            $arr_font['color']->setAttribute('w:val',$color);
        }

        if (isset($font['super']) && isset($font['sub'])) throw new \Exception('上标和下标不能同时设置');

        //上标
        if(isset($font['super'])){
            $arr_font['vAlign']=$this->dom->createElement('w:vertAlign');
            $arr_font['vAlign']->setAttribute('w:val','superscript');
        }

        //下标
        if(isset($font['sub'])){
            $arr_font['vAlign']=$this->dom->createElement('w:vertAlign');
            $arr_font['vAlign']->setAttribute('w:val','subscript');
        }

        foreach ($arr_font as $child){
            $this->rPr->appendChild($child);
        }

        return $this->rPr;
    }

    /**
     * @param array $font
     * @return \DOMElement
     */
    protected function setFontName($font)
    {
        $ele_name=$this->dom->createElement('w:rFonts');
        if (!((isset($font['name_zh']) && isset($font['name_en'])) || isset($font['name']))){
            $zh=$this->defaultFont['name_zh'];
            $en=$this->defaultFont['name_en'];
        }else{
            isset($font['name_zh']) ? $zh=$font['name_zh'] : $zh = $font['name'];
            isset($font['name_en']) ? $en=$font['name_en'] : $en = $font['name'];
        }
        $ele_name->setAttribute('w:eastAsia',$zh);
        $ele_name->setAttribute('w:ascii',$en);
        $ele_name->setAttribute('w:hAnsi',$en);
        $ele_name->setAttribute('w:cs',$en);

        return $ele_name;
    }


}